<?php
/**
 * Created by PhpStorm.
 * User: wen
 * Date: 2017/6/21
 * Time: 11:41
 */
namespace Biz;

class Order extends \Base\BaseBiz{
    private $limit_time = 0;
    /*
     * 生成订单
     * @param $data
     * @Return NULL
     */
    public function createOrder($data,$products){

        $order_cls = new \Orm\Order\OrderClass();
        $order_cls['order_code'] = date('YmdH') . mt_rand(000, 999);
        $order_cls['article_id'] = $data['article_id'];
        $order_cls['seller_uid'] = $data['seller_uid'];
        $order_cls['buyer_uid'] =$data['buyer_uid'];
        $order_cls['total_price'] = $this->getTotal($products);
        $order_cls['pay_status'] = \Orm\Order\OrderClass::NONPAYMENT;
        $order_cls->beginTransaction();
        try {
            $order_cls->insert();
            if ($order_cls['id'] && $products) {
                $order_detail = new \Orm\OrderDetail\OrderDetailClass();
                foreach ($products as $key => $val){
                    $p_res = $this->getProductRow($val->id);
                    $total_price = $p_res['unit_price']*$val->amount; //商品总价
                    $order_detail['order_id'] = $order_cls['id'];
                    $order_detail['product_id'] = $p_res['id'];
                    $order_detail['unit_price'] = $p_res['unit_price'];
                    $order_detail['amount'] = $val->amount;
                    $order_detail['total_price'] = $total_price;
                    $order_detail ->insert();
                }
            }
        } catch (\Exception $e) {
            $order_cls->rollBack();
            throw new \Exception($e->getMessage());
        }
        $order_cls->commit();
        return $order_cls['id'];
    }
    /*
     *根据商品ID查找商品
     * 参数 商品ID
     */
    public function getProductRow($id){
        if (empty($id)) {
            return \Orm\Product\ProductClass::PRODUCT_ID_NULL;
        }
        try{
            $product_cls = new \Orm\Product\ProductClass();
            $product_cls->loadById($id);
            if ($product_cls) {
                return $product_cls;
            }
            return array();
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }
    /*
     *获取订单总价
     */
    public function getTotal($products){
        $total = 0;
        $order_detail = new \Orm\OrderDetail\OrderDetailClass();
        foreach ($products as $key => $val){
            $p_res = $this->getProductRow($val->id);
            $total += $p_res['unit_price']*$val->amount; //商品总价
        }
        return $total;
    }
    /*
     *使用Orderid查询订单信息
     */
    public function getOrderInfoById($order_id){
        if (empty($order_id)) {
            return \Orm\Order\OrderClass::ID_NULL;
        }
        try{
            $order_info = array();
            $order_cls = new \Orm\Order\OrderClass();
            $order_cls -> loadById($order_id);
            $order_info['order_info'] = $order_cls->objToArray($order_cls);
            //获取流水号信息
            $transaction = new \Biz\Transaction();
            $transaction_cls = new \Orm\Transaction\TransactionClass();
            $transaction_res = $transaction ->getTransactionByOrderId($order_id);
            $order_info['order_transaction'] = $transaction_cls->objToArray($transaction_res['lists'][0]);
            if ($order_info) {
                return  $order_info;
            }
            return array();
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }
    /*
     * 按照order_id查询订单状态
     */
    public function getOrderStatusById($order_id){
        if (empty($order_id)) {
            return \Orm\Order\OrderClass::ID_NULL;
        }
        $order_cls = new \Orm\Order\OrderClass();
        $order_cls ->loadById($order_id);
        $result = false;
        if ($order_cls['pay_status'] == \Orm\Order\OrderClass::NONPAYMENT) {
            return $result= true;
        }
    }


    /*
     *根据openid查找我的购买列表
     * 参数 open_id
     */
    public function getMyPurchaseLsit($buyer_uid){
        if(empty($buyer_uid)){
            return array();
        }
        $orderlist = new \Orm\Order\OrderList();
        $orderlist['where'] = sprintf("`buyer_uid` in(%s)",$buyer_uid);
        $orderlist['order'] = "`insert_time` ASC";
        $orderlist ->load();
        $delivery_detail_list = $orderlist['lists'];
        if($delivery_detail_list){
            return $delivery_detail_list;
        }
        return array();
    }
    /*
     * 根据orderid查询订单详情
     */
    public function getOrderIdByDateil($order_id){
        if (empty($order_id)) {
            return \Orm\Order\OrderClass::ID_NULL;
        }
        try{
            $order_cls = new \Orm\Order\OrderClass();
            $product_img = new \Orm\ProductImg\ProductImgClass();
            $order_detail_cls = new \Orm\OrderDetail\OrderDetailClass();
            $product_image_list = new \Orm\ProductImg\ProductImgList();
            $product_cls = new \Orm\Product\ProductClass();
            /*订单信息*/
            $order_dateil =array();
            $order_dateil_ =array();
            $order_body = array();
            $product_img_dateil = array();
            $product_info = array();
            $img = array();
            $order_cls ->loadById($order_id);
            /*转数组*/
            $order_body = $order_cls->objToArray($order_cls);
            /*订单详情*/
            $order_dateil_list = new \Orm\OrderDetail\OrderDetailList();
            $order_dateil_list['where'] = 'order_id='.$order_id;
            $order_dateil_list ->load();
            /*获取商品详情*/
            foreach ($order_dateil_list['lists'] as $key => $val){
                /*转数组*/
                $order_dateil[$key] = $order_detail_cls ->objToArray($val);
            }
            /*用商品详情中的product_id查询img和商品信息*/
            foreach ($order_dateil as $key => $val){
                /*查询商品信息*/
                $product_cls ->loadById($val['product_id']);
                $product_info = $product_cls->objToArray($product_cls);

                /*查询商品图片*/
                $product_image_list['where'] = 'product_id = '.$val['product_id'];
                $product_image_list->load();
                foreach ( $product_image_list['lists'] as $v){
                    $img = $product_img ->objToArray($v);
                }
                $product_info ['img'] = $img;
                $order_dateil_[$key] = $val;
                $order_dateil_[$key]['product'] = $product_info;

            }
            $order_body['dateil'] = $order_dateil_;
            return $order_body;
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }

}